package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.apis.wemedia.IWemediaClient;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.Authdto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.ApUserRealnameService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
@Slf4j
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {

    /**
     * 查询用户审核列表
     *
     * @return
     */
    public ResponseResult authList(Authdto dto) {
        //1.检查参数
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();
        //分页查询
        IPage page = new Page(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<ApUserRealname> queryWrapper = new LambdaQueryWrapper<>();
        //按照状态查询
        if (dto.getStatus() != null) {
            queryWrapper.eq(ApUserRealname::getStatus, dto.getStatus());
        }
        //按照创建时间倒序排序
        queryWrapper.orderByDesc(ApUserRealname::getCreatedTime);
        //查找集合封装到page中
        //int total=list(queryWrapper).size();
        page = page(page, queryWrapper);
        //返回结果
        ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;
    }

    /**
     * 用户审核失败--驳回
     *
     * @param dto
     * @return
     */
    public ResponseResult authFail(Authdto dto) {
        //1.检查参数
        if (dto.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //驳回理由不可为空
        if (dto.getMsg() == null && StringUtils.isBlank(dto.getMsg())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "驳回理由不可为空");
        }
        //2.更改审核内容
        ApUserRealname apUserRealname = getById(dto.getId());
        apUserRealname.setStatus((short) 2);
        apUserRealname.setUpdatedTime(new Date());
        apUserRealname.setReason(dto.getMsg());
        updateById(apUserRealname);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Autowired
    private ApUserMapper apUserMapper;

    /**
     * 用户审核成功--通过
     *
     * @param dto
     * @return
     */
    @GlobalTransactional
    public ResponseResult authPass(Authdto dto) {
        //1.检查参数
        if (dto.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.更改用户审核的数据
        ApUserRealname apUserRealname = getById(dto.getId());
        apUserRealname.setStatus((short) 9);
        apUserRealname.setUpdatedTime(new Date());
        updateById(apUserRealname);
        //3.feign远程调用wemedia创建账号--该账号的用户名和密码与app一致
        Integer userId = apUserRealname.getUserId();
        ApUser apUser = apUserMapper.selectById(userId);
        ResponseResult responseResult = CreatWemediaCount(apUser);
        if (responseResult.getCode() != 200) {
            throw new RuntimeException("ApUserRealnameServiceImpl---远程调用--创建账户失败");
        }
        //创建账户成功后回填ap_user相关信息
        apUser.setIdentityAuthentication(true);
        apUser.setFlag((short) 1);
        apUserMapper.updateById(apUser);
        //4.返回结果
        return responseResult;
    }

    @Autowired
    private IWemediaClient iWemediaClient;

    /**
     * feign远程调用创建自媒体账户
     *
     * @param apUser
     */
    private ResponseResult CreatWemediaCount(ApUser apUser) {
        WmUser wmUser = new WmUser();
        //数据拷贝
        BeanUtils.copyProperties(apUser, wmUser,"Id");
        wmUser.setApUserId(apUser.getId());
        wmUser.setStatus(9);
        wmUser.setCreatedTime(new Date());
        return iWemediaClient.creatCount(wmUser);
    }
}
